/*************************************************
*           BK Emulator v3.8 for Windows         *
*************************************************/

/*
File: TapeControlDlg.cpp
Version: 1.0.0
Written by: Yuriy Kalmykov <kalmykov@stoik.com>
    Copyright (c) 2002-2004 Yuriy Kalmykov

    BK Emulator is a program emulated hardware environment for running
code for BK 0010(01) in different configurations.

    This code may be used in compiled form in any way you desire.
This file or it's parts can't be redistributed without the authors
written consent, but can be modified for your private needs.
    Providing that this notice and the authors name and all copyright
notices remains intact.

    Please, an email me to know how you are using it and where. You can
ask me for any information about this below code or any attendant
knowledge.

    This file is provided "as is" with no expressed or implied warranty.
The author accepts no liability for any damage or loss of business that
this product may cause.
*/


// TapeControlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TapeControlDlg.h"
#include "afxdialogex.h"
#include "Defines.h"
#include "Globals.h"
#include "Tape.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTapeControlDlg dialog

CTapeControlDlg::CTapeControlDlg(CTape *pTape, CWnd *pParent /*=NULL*/)
	: CDialogEx(CTapeControlDlg::IDD, pParent),
	  m_pTape(pTape),
	  m_bAutoBeginDetection(FALSE),
	  m_bAutoEnddetection(FALSE)
{
}

void CTapeControlDlg::DoDataExchange(CDataExchange *pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_TC_AUTOBEGIN, m_bAutoBeginDetection);
	DDX_Check(pDX, IDC_TC_AUTOEND, m_bAutoEnddetection);
}

BEGIN_MESSAGE_MAP(CTapeControlDlg, CDialogEx)
	ON_BN_CLICKED(IDC_TC_RECORD, OnTcRecord)
	ON_BN_CLICKED(IDC_TC_STOP, OnTcStop)
	ON_WM_TIMER()
	ON_WM_DESTROY()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTapeControlDlg message handlers

BOOL CTapeControlDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	m_iconRecordActive = ::LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_TCTRL_RECORD));
	m_iconRecordPassive = ::LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_TCTRL_RECORD_DARK));
	m_iconRecordStop = ::LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_TCTRL_STOP));
	(reinterpret_cast<CButton *>(GetDlgItem(IDC_TC_RECORD)))->SetIcon(m_iconRecordPassive);
	(reinterpret_cast<CButton *>(GetDlgItem(IDC_TC_STOP)))->SetIcon(m_iconRecordStop);
	UpdateData(FALSE);
	SetTimer(0, 300, NULL);
	m_Resizer.Init(this);
	return TRUE; // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CTapeControlDlg::OnOK()
{
}


void CTapeControlDlg::OnCancel()
{
}


void CTapeControlDlg::OnTcRecord()
{
	UpdateData(TRUE);
	CButton *pBtn = reinterpret_cast<CButton *>(GetDlgItem(IDC_TC_RECORD));

	if (IsDlgButtonChecked(IDC_TC_RECORD))
	{
		pBtn->SetIcon(m_iconRecordActive);
		m_pTape->StartRecord(m_bAutoBeginDetection, m_bAutoEnddetection);
	}
	else
	{
		pBtn->SetIcon(m_iconRecordPassive);
		m_pTape->StopRecord();
		ShowSaveDialog();
	}

	GetParent()->SetFocus();
}


void CTapeControlDlg::OnTcStop()
{
	CButton *pBtn = reinterpret_cast<CButton *>(GetDlgItem(IDC_TC_RECORD));

	if (pBtn->GetCheck())
	{
		pBtn->SetCheck(0);
	}

	if (m_pTape->IsPlaying())
	{
		m_pTape->StopPlay();
	}

	if (m_pTape->IsRecording())
	{
		pBtn->SetIcon(m_iconRecordPassive);
		m_pTape->StopRecord();
		ShowSaveDialog();
	}
}


void CTapeControlDlg::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == 0)
	{
		CButton *pBtn = reinterpret_cast<CButton *>(GetDlgItem(IDC_TC_RECORD));
		BOOL bPress = pBtn->GetCheck();

		if (bPress && m_pTape && (!m_pTape->IsRecording()))
		{
			pBtn->SetCheck(0);
			OnTcRecord();
		}
	}
	else
	{
		CDialogEx::OnTimer(nIDEvent);
	}
}


void CTapeControlDlg::ShowSaveDialog()
{
	CString strFilterTape;
	strFilterTape.LoadString(IDS_FILEFILTER_TAPE);
	CString strWaveExt;
	strWaveExt.LoadString(IDS_FILEEXT_WAVE);
	CString strTapeExt;
	strTapeExt.LoadString(IDS_FILEEXT_TAPE);
	CFileDialog dlg(FALSE, strTapeExt, NULL,
	                OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER,
	                strFilterTape, this);
	dlg.GetOFN().lpstrInitialDir = m_strTapePath;

	if (dlg.DoModal() == IDOK)
	{
		CString strFilePath = dlg.GetPathName();
		CString strExt = g_Config.GetFileExt(strFilePath);

		if (strExt == strWaveExt)
		{
			m_pTape->SaveWaveFile(strFilePath);
		}
		else
		{
			m_pTape->SaveMSFFile(strFilePath);
		}
	}

	m_strTapePath = g_Config.GetFilePath(dlg.GetPathName());
}


void CTapeControlDlg::PressRecordButton()
{
	SendDlgItemMessage(IDC_TC_RECORD, BM_CLICK);
}

void CTapeControlDlg::OnDestroy()
{
	if (m_iconRecordActive)
	{
		DestroyIcon(m_iconRecordActive);
	}

	if (m_iconRecordPassive)
	{
		DestroyIcon(m_iconRecordPassive);
	}

	if (m_iconRecordStop)
	{
		DestroyIcon(m_iconRecordStop);
	}

	CDialogEx::OnDestroy();
}


CSize CTapeControlDlg::GetMinSize()
{
	CSize size;
	MINMAXINFO mmi;
	m_Resizer.GetMinMaxInfo(&mmi);
	size.cx = mmi.ptMinTrackSize.x;
	size.cy = mmi.ptMinTrackSize.y;
	return size;
}
